package com.company.bookstore.validator.user_admin_module;

import com.company.bookstore.entities.user_admin_module.Address;
import com.company.bookstore.entities.user_admin_module.User;
import com.company.bookstore.exception.user_admin_module.CustomException;
import com.company.bookstore.qo.user_admin_module.AddressQo;
import com.company.bookstore.service.user_admin_module.AddressService;
import com.company.bookstore.service.user_admin_module.UserService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.regex.Pattern;

@Component
public class AddressQoUpdateValidator extends AbstractValidatorHandler implements InitializingBean{
    @Resource
    private AddressService addressService;
    @Resource
    private UserService userService;
    static final Pattern CITY_PATTERN = Pattern.compile("^[\\u4e00-\\u9fa5_a-zA-Z0-9-]{0,20}");
    static final Pattern COUNTRY_PATTERN = Pattern.compile("^[\\u4e00-\\u9fa5_a-zA-Z0-9-]{0,10}");
    static final Pattern PROVINCE_PATTERN = Pattern.compile("^[\\u4e00-\\u9fa5_a-zA-Z0-9-]{0,20}");
    static final Pattern TOWN_PATTERN = Pattern.compile("^[\\u4e00-\\u9fa5_a-zA-Z0-9-]{0,20}");
    static final Pattern DETAIL_PATTERN = Pattern.compile("^[\\u4e00-\\u9fa5_a-zA-Z0-9-]+");

    @Override
    public boolean doValidator(Object object) throws NoSuchMethodException, CustomException {

        AddressQo addressQo = (AddressQo)object;
        if (addressQo.getId() == null){
            throw  new CustomException("没有指定地址",405);
        }
        Address address = addressService.getById(addressQo.getId());
        if (address == null){
            throw  new CustomException("没有找到指定地址",405);
        }
        String city = addressQo.getCity();
        String town = addressQo.getTown();
        String country = addressQo.getCountry();
        Integer deleted = addressQo.getDeleted();
        String detail = addressQo.getDetail();
        String phone = addressQo.getPhone();
        String province = addressQo.getProvince();
        String username = addressQo.getUsername();
        Integer fkUserId = addressQo.getFkUserId();

        if (username!=null){
            if (! AddressRegisterValidator.NAME_PATTERN.matcher(username).matches()){
                throw  new CustomException("姓名格式不正确，要求0-20位中文,字母,不能含有标点符号,数字",405);
            }
        }
        if (!StringUtils.isEmpty(city)){
            if (! CITY_PATTERN.matcher(city).matches()){
                throw  new CustomException("城市名称不正确，要求0-20位中文，字母，数字，不能含有标点符号",405);
            }
        }
        if (!StringUtils.isEmpty(town)){
            if (! TOWN_PATTERN.matcher(town).matches()){
                throw  new CustomException("城镇名称不正确，要求0-20位中文，字母，数字，不能含有标点符号",405);
            }
        }
        if (!StringUtils.isEmpty(country)){
            if (! COUNTRY_PATTERN.matcher(country).matches()){
                throw  new CustomException("国家名称不正确，要求0-20位中文，字母，数字，不能含有标点符号",405);
            }
        }
        if (!StringUtils.isEmpty(province)){
            if (! PROVINCE_PATTERN.matcher(province).matches()){
                throw  new CustomException("国家名称不正确，要求0-20位中文，字母，数字，不能含有标点符号",405);
            }
        }
        if (!StringUtils.isEmpty(phone)){
            if (! UserQoUpdateRegisterValidator.USER_PHONE_PATTERN.matcher(phone).matches()){
                throw  new CustomException("账号手机号不正确，要求数字11到12位",405);
            }
        }
        if (!StringUtils.isEmpty(detail)){
            if (! DETAIL_PATTERN.matcher(detail).matches()){
                throw  new CustomException("详细地址不正确，要求不允许有特殊标点符号，可以使用-_",405);
            }
        }
        if (deleted!=null){
            if (deleted!=0&&deleted!=1){
                throw  new CustomException("地址状态不正确",405);
            }
        }

        if (fkUserId != null){
            User byId = userService.getById(fkUserId);
            if (byId == null){
                throw new CustomException("没有该用户",405);
            }
        }

        return true;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        ValidatorFactory.register(this.getClass().getName(),this);
    }
}
